package com.android.happymall.fragment;


import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.happymall.MainActivity;
import com.android.happymall.R;
import com.android.happymall.activity.BaseObject;
import com.android.happymall.activity.MainTab;
import com.android.happymall.activity.mine.DataEditActivity;
import com.android.happymall.activity.mine.LoginActivity;
import com.android.happymall.model.member.MemberInfoData;
import com.android.happymall.model.member.MemberInfoDataCallback;
import com.android.happymall.utils.ConstantsUrl;
import com.android.happymall.utils.HttpUtils;
import com.android.happymall.utils.LoginState;
import com.android.happymall.utils.MyLog;
import com.android.happymall.utils.ObjectUtils;
import com.android.happymall.view.dialog.MainMsg;
import com.elbbbird.android.socialsdk.SocialSDK;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.android.happymall.R.id.mine_money;


/**
 * 个人中心
 */
public class Fragment5 extends Fragment implements MainActivity.DataChange {

    @BindView(R.id.user_avatar)
    ImageView userAvatar;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.num_dfk)
    TextView numDfk;
    @BindView(R.id.num_dfh)
    TextView numDfh;
    @BindView(R.id.num_dsh)
    TextView numDsh;
    @BindView(R.id.num_dpj)
    TextView numDpj;
    @BindView(R.id.num_dtk)
    TextView numDtk;
    @BindView(R.id.num_msg)
    TextView num_msg;
    @BindView(mine_money)
    LinearLayout mineMoney;
    @BindView(R.id.mine_money_total)
    TextView mineMoneyTotal;
    @BindView(R.id.mine_money_use)
    TextView mineMoneyUse;
    @BindView(R.id.mine_exit)
    Button mineExit;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    private FragmentActivity mcontext;
    private View view;

    private boolean networkConnected;
    private String login_type;
    private String commission;
    private String nickname;
    private String photo;
    private String mobile;


    public Fragment5() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_fragment5, container, false);

        ButterKnife.bind(this, view);


        mcontext = getActivity();

        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        ((MainActivity) mcontext).setData(this);


        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        initData();
    }

    @Override
    public void setDataChange(int curr) {
        initData();
    }

    private void initData() {

        if (LoginState.getInstance().isLogin(mcontext)) {
            nickname = LoginState.getInstance().getUserInfo(mcontext, 0);
            photo = LoginState.getInstance().getUserInfo(mcontext, 1);
            login_type = LoginState.getInstance().getUserInfo(mcontext, 3);
            userName.setText(nickname);
            ObjectUtils.photoCircle(mcontext, photo, userAvatar);
            mineExit.setVisibility(View.VISIBLE);
            GetDataNumber();
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
            mineExit.setVisibility(View.GONE);
            userName.setText("未登录");
            userAvatar.setImageResource(R.mipmap.avater);
            numDfk.setVisibility(View.INVISIBLE);
            numDfh.setVisibility(View.INVISIBLE);
            numDsh.setVisibility(View.INVISIBLE);
            numDpj.setVisibility(View.INVISIBLE);
            numDtk.setVisibility(View.INVISIBLE);
            num_msg.setVisibility(View.INVISIBLE);
        }


    }


    //
    private void GetDataNumber() {
        String address = ConstantsUrl.domain + ConstantsUrl.MEMBERORDINGDANGNUM;
        MyLog.e("地址", address);
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            String getid = LoginState.getInstance().getid(mcontext);
            String getpd = LoginState.getInstance().getpd(mcontext);
            HashMap<String, String> header = new HashMap<>();
            header.put("UID", getid);
            header.put("UPSW", getpd);
            HttpUtils.get(address, header, null, new MemberInfoDataCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(MemberInfoData response, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);

                    if (response.getCode() == 0) {



                    } else {
//                        noData.setVisibility(View.VISIBLE);
//                        noDataTv.setText(response.getMsg());
//                        noDataTv.setVisibility(View.VISIBLE);
                    }
                }

            });
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
        }


    }

    private void UpdataInfo(String uname, String avatar, String uid) {
        SharedPreferences sp = mcontext.getSharedPreferences(ConstantsUrl.USERDATA, 0);
        SharedPreferences.Editor edit = sp.edit();
        edit.putString(ConstantsUrl.USERDATA_NAME, uname);
        edit.putString(ConstantsUrl.USERDATA_AVATAR, avatar);
        edit.putString(ConstantsUrl.USERDATA_UID, uid);
        edit.commit();
    }

    @OnClick({R.id.user_avatar, R.id.user_ll, R.id.message, R.id.set, R.id.mine_order_all, R.id.mine_dingdang_1, R.id.mine_dingdang_2,
            R.id.mine_dingdang_3, R.id.mine_dingdang_4, R.id.mine_dingdang_5, mine_money, R.id.mine_money_cash,
            R.id.mine_group, R.id.mine_lottery, R.id.mine_collect, R.id.mine_coupon, R.id.mine_address, R.id.mine_message,
            R.id.mine_rank, R.id.mine_fans, R.id.mine_question, R.id.mine_about, R.id.mine_exit, R.id.click_resetnetwork_refresh})
    public void onClick(View view) {

        Intent intent_data = new Intent(mcontext, DataEditActivity.class);
        switch (view.getId()) {
            case R.id.user_avatar:
                if (LoginState.getInstance().isLogin(mcontext)) {
                    intent_data.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_DATAEDIT_AVATAR, photo);
                    intent_data.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_DATAEDIT_NICK, nickname);
                    intent_data.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_DATAEDIT_PHONE, mobile);
                    startActivity(intent_data);

                } else {
                    startActivity(new Intent(mcontext, LoginActivity.class));
                }
                break;
            case R.id.user_ll:
                if (LoginState.getInstance().isLogin(mcontext)) {

                    return;
                }
                startActivity(new Intent(mcontext, LoginActivity.class));
                break;
            case R.id.mine_message:
            case R.id.message:
                if (LoginState.getInstance().isLogin(mcontext)) {

                    ToObject("我的消息", 0);
                } else {
                    startActivity(new Intent(mcontext, LoginActivity.class));
                }
                break;
            case R.id.set:
                if (LoginState.getInstance().isLogin(mcontext)) {
                    intent_data.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_DATAEDIT_AVATAR, photo);
                    intent_data.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_DATAEDIT_NICK, nickname);
                    intent_data.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_MEMBER_DATAEDIT_PHONE, mobile);
                    startActivity(intent_data);
                } else {
                    startActivity(new Intent(mcontext, LoginActivity.class));
                }
                break;
            case R.id.mine_order_all:
                ToOrder(0, 4);
                break;
            case R.id.mine_dingdang_1:
                ToOrder(1, 4);
                break;
            case R.id.mine_dingdang_2:
                ToOrder(2, 4);
                break;
            case R.id.mine_dingdang_3:
                ToOrder(3, 4);
                break;
            case R.id.mine_dingdang_4:
                ToOrder(4, 4);
                break;
            case R.id.mine_dingdang_5:
                ToOrder(5, 4);
                break;
            case R.id.mine_money:
                if (LoginState.getInstance().isLogin(mcontext)) {

                    ToObject("我的佣金", 0);
                } else {
                    startActivity(new Intent(mcontext, LoginActivity.class));
                }
                break;
            case R.id.mine_money_cash:
                if (LoginState.getInstance().isLogin(mcontext)) {

                    ToObject("提现", 3);
                } else {
                    startActivity(new Intent(mcontext, LoginActivity.class));
                }
                break;
            case R.id.mine_group:
                ToOrder(0, 5);
                break;
            case R.id.mine_lottery:
                if (LoginState.getInstance().isLogin(mcontext)) {
                    ToObject("我的抽奖", 0);
                } else {
                    startActivity(new Intent(mcontext, LoginActivity.class));
                }
                break;
            case R.id.mine_collect:
                ToOrder(0, 6);
                break;
            case R.id.mine_coupon:
                ToOrder(0, 7);
                break;
            case R.id.mine_address:
                if (LoginState.getInstance().isLogin(mcontext)) {
                    ToObject("收货地址", 0);
                } else {
                    startActivity(new Intent(mcontext, LoginActivity.class));
                }
                break;

            case R.id.mine_rank:
                if (LoginState.getInstance().isLogin(mcontext)) {
                    ToObject("拼团王", 0);
                } else {
                    startActivity(new Intent(mcontext, LoginActivity.class));
                }
                break;
            case R.id.mine_fans:
                if (LoginState.getInstance().isLogin(mcontext)) {
                    ToObject("我的粉丝", 0);
                } else {
                    startActivity(new Intent(mcontext, LoginActivity.class));
                }
                break;
            case R.id.mine_question:
                ToObject("", 1);
                break;
            case R.id.mine_about:
                ToObject("关于我们", 0);
                break;
            case R.id.mine_exit:
                final MainMsg mainMsg = new MainMsg(mcontext);
                mainMsg.show(mcontext.getWindow().getDecorView(), 0, "是否退出登录？");
                mainMsg.setOnSetSureClickListener(new MainMsg.OnSetSureClickListener() {
                    @Override
                    public void onSure() {
                        if (login_type.equals("1")) {
                            SocialSDK.revokeWeibo(getActivity());

                        } else if (login_type.equals("2")) {

                            SocialSDK.revokeWeChat(getActivity());
                        } else if (login_type.equals("3")) {

                            SocialSDK.revokeQQ(getActivity());
                        }
                        SharedPreferences sp = mcontext.getSharedPreferences(ConstantsUrl.USERDATA, 0);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.clear();
                        editor.commit();
                        mineExit.setVisibility(View.GONE);
                        initData();
                        mainMsg.close();
                    }
                });

                break;
            case R.id.click_resetnetwork_refresh:
                ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
                initData();
                break;
        }
    }

    private void ToObject(String title, int type) {
        Intent intent_q = new Intent(getActivity(), BaseObject.class);
        intent_q.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE, title);
        switch (type) {
            case 0:
                break;
            case 1:
                intent_q.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_WEB_URL, "http://pin.lnest.cc/content/index/22");
                break;
            case 2:
                intent_q.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_WEB_URL, "http://pin.lnest.cc/content/index/23");
                break;
            case 3:
                intent_q.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_NEED_ID, commission);
                break;

        }

        startActivity(intent_q);
    }

    private void ToOrder(int tab, int type) {
        if (LoginState.getInstance().isLogin(mcontext)) {

            Intent intent = new Intent(mcontext, MainTab.class);
            intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_ID, tab);
            intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TAB_SORT_SHOW_TYPE, type);

            startActivity(intent);
        } else {
            startActivity(new Intent(mcontext, LoginActivity.class));
        }
    }


}
